<template>
  <div class="password" @click="showxuanze = false;">
    <ComHeader title="支付密码" leftArrow="true" />
    <div style="padding-top: 54px;"></div>
    <!-- 进度条 -->
    <div>
      <div class="progress flex-row-between">
        <div class="img-box flex-column-center ">
          <img src="../../static/ethel_img/icon_checked.png" alt="" :class="step>=1?'':'hidden'">
        </div>
        <div class="line flex-num-one" :class="step>=1?'line-red':''"></div>
        <div class="img-box flex-column-center">
          <img src="../../static/ethel_img/icon_checked.png" alt="" :class="step>=2?'':'hidden'">
        </div>
        <div class="line flex-num-one" :class="step>=2?'line-red':''"></div>
        <div class="img-box flex-column-center">
          <img src="../../static/ethel_img/icon_checked.png" alt="" :class="step>=3?'':'hidden'">
        </div>
      </div>
      <div class="progress-text-box flex-row-between">
        <div class="" :class="step>=1?'text-red':''">验证身份</div>
        <div class="" :class="step>=2?'text-red':''">设置密码</div>
        <div class="" :class="step>=3?'text-red':''">确认密码</div>
      </div>
    </div>

    <!-- 验证码盒子 -->
    <div :class="{'hidden':step!=1}">
      <ul class="flex-column-start" :class="{'hidden':!isShowOldPassWord}">
        <li class="flex-row-between code-li">
          <div>
            <input type="tel" placeholder="请输入手机号" v-model="phonenumber" minlength="11" maxlength="11" readonly="readonly">
          </div>
          <div class="getcode-btn flex-column-center" @click="fnGetCode()" id="getcode-btn" ref="code"
            :class="{'btn-disabled':isdisabled}">获取验证码</div>
        </li>
        <li class="flex-row-between code-li">
          <div>
            <input type="text" placeholder="请输入验证码" v-model="code">
          </div>
          <div @click="fnClearCode()">
            <img src="../../static/ethel_img/icon_cloes.png" alt="" class="code-img">
          </div>
        </li>
      </ul>
      <div :class="{'hidden':isShowOldPassWord}">
        <div class="input-password-box">
          <div class="password-text text-c old-password">请输入原支付密码</div>
          <ul class="password-ul flex-row-between pos-relative">
            <li class="password-li flex-column-center flex-num-one">
              <span :class="{'password-span':inputMask[0]}"></span>
            </li>
            <li class="password-li flex-column-center flex-num-one">
              <span :class="{'password-span':inputMask[1]}"></span>
            </li>
            <li class="password-li flex-column-center flex-num-one">
              <span :class="{'password-span':inputMask[2]}"></span>
            </li>
            <li class="password-li flex-column-center flex-num-one">
              <span :class="{'password-span':inputMask[3]}"></span>
            </li>
            <li class="password-li flex-column-center flex-num-one">
              <span :class="{'password-span':inputMask[4]}"></span>
            </li>
            <li class="password-li flex-column-center flex-num-one" style="border: 0px;">
              <span :class="{'password-span':inputMask[5]}"></span>
            </li>
            <div class="pos-absolute input-mask" @click="fnInput()"></div>
          </ul>
        </div>

        <div class="password-box flex-display flex-display-column" id="layer">
          <div class="psd-box flex-num-one flex-display flex-center-align  bounceInUp">
          </div>
          <div class="keyboard  bounceInDown" id="keyboard">
            <ul class="flex-display flex-warp">
              <li class="border-top" tapmode @click="fnHandleSetPassword(1)">1</li>
              <li class="border-top" tapmode @click="fnHandleSetPassword(2)">2</li>
              <li class="border-top" tapmode @click="fnHandleSetPassword(3)">3</li>
              <li tapmode @click="fnHandleSetPassword(4)">4</li>
              <li tapmode @click="fnHandleSetPassword(5)">5</li>
              <li tapmode @click="fnHandleSetPassword(6)">6</li>
              <li tapmode @click="fnHandleSetPassword(7)">7</li>
              <li tapmode @click="fnHandleSetPassword(8)">8</li>
              <li tapmode @click="fnHandleSetPassword(9)">9</li>
              <li class="ba-li border-none"></li>
              <li class="border-none" tapmode @click="fnHandleSetPassword(0)">0</li>
              <li class="ba-li border-none" tapmode @click="fnHandleSetPassword('删除')">删除</li>
            </ul>
          </div>
        </div>
      </div>

      <div class="checkpassword flex-column-center">
        <span @click="isShowOldPassWord = true;phonenumber='';code='';inputMask='';"
          v-if="!isShowOldPassWord ">使用绑定手机验证身份></span>
        <span @click="isShowOldPassWord = false;phonenumber='';code='';inputMask='';"
          v-if="isShowOldPassWord && isShowOldInput">使用支付密码验证身份></span>
      </div>
    </div>

    <!-- 密码输入框 -->
    <div class="input-password-box" :class="{'hidden':step==1}">
      <div class="password-text text-c" v-if="step==3">请确认6位支付密码</div>
      <div class="password-text text-c" v-else>请输入6位支付密码</div>
      <ul class="password-ul flex-row-between pos-relative">
        <li class="password-li flex-column-center flex-num-one">
          <span :class="{'password-span':inputMask[0]}"></span>
        </li>
        <li class="password-li flex-column-center flex-num-one">
          <span :class="{'password-span':inputMask[1]}"></span>
        </li>
        <li class="password-li flex-column-center flex-num-one">
          <span :class="{'password-span':inputMask[2]}"></span>
        </li>
        <li class="password-li flex-column-center flex-num-one">
          <span :class="{'password-span':inputMask[3]}"></span>
        </li>
        <li class="password-li flex-column-center flex-num-one">
          <span :class="{'password-span':inputMask[4]}"></span>
        </li>
        <li class="password-li flex-column-center flex-num-one" style="border: 0px;">
          <span :class="{'password-span':inputMask[5]}"></span>
        </li>
        <div class="pos-absolute input-mask" @click="fnInput()"></div>
      </ul>
    </div>

    <div class="password-box flex-display flex-display-column" :class="{'hidden':step==1}" id="layer">
      <div class="psd-box flex-num-one flex-display flex-center-align  bounceInUp">
      </div>
      <div class="keyboard  bounceInDown" id="keyboard">
        <ul class="flex-display flex-warp">
          <li class="border-top" tapmode @click="fnHandleSetPassword(1)">1</li>
          <li class="border-top" tapmode @click="fnHandleSetPassword(2)">2</li>
          <li class="border-top" tapmode @click="fnHandleSetPassword(3)">3</li>
          <li tapmode @click="fnHandleSetPassword(4)">4</li>
          <li tapmode @click="fnHandleSetPassword(5)">5</li>
          <li tapmode @click="fnHandleSetPassword(6)">6</li>
          <li tapmode @click="fnHandleSetPassword(7)">7</li>
          <li tapmode @click="fnHandleSetPassword(8)">8</li>
          <li tapmode @click="fnHandleSetPassword(9)">9</li>
          <li class="ba-li border-none"></li>
          <li class="border-none" tapmode @click="fnHandleSetPassword(0)">0</li>
          <li class="ba-li border-none" tapmode @click="fnHandleSetPassword('删除')">删除</li>
        </ul>
      </div>
    </div>

    <!-- 确认按钮 -->
    <div class="flex-column-center commit" :class="{'hidden':step==2}" @click="fnCommit()">
      确定
    </div>
  </div>
</template>
<style>
  .layer {
    width: 100%;
    height: 100%;
    left: 0px;
    top: 0px;
    bottom: 0px;
    right: 0px;
    background: #fff;
    position: fixed;
  }

  .psd-box {
    box-sizing: border-box;
    padding: 0 15px;
    background: rgba(0, 0, 0, 0.5);
  }

  .psd-box>div {
    background: #fff;
    border-radius: 5px;
    padding: 10px;
    width: 80%;
    margin: 0 auto;
    position: relative;
    text-align: center;
  }

  .psd-box>div>.close {
    width: 100%;
    height: 30px;
    text-align: center;
    border-bottom: 1px solid #e5e5e5;
    margin-bottom: 10px;
    position: relative;
  }

  .psd-box>div>.close i {
    position: absolute;
    left: 0px;
    width: 20px;
    height: 20px;
    /* background: url(../../image/v1/address/icon_close.png) center center no-repeat; */
    background-size: 100% 100%;
  }

  .psd-box>div>span {
    font-size: 20px;
    margin-bottom: 10px;
  }

  .psd-box__warp {
    width: 100%;
    height: 44px;
    box-sizing: border-box;
    border: 1px solid rgba(238, 238, 238, 1);
    ;
  }

  .psd-box__warp li {
    height: 100%;
    text-align: center;
    font-size: 18px;
    line-height: 42px;
    border-right: 1px solid rgba(238, 238, 238, 1);
    ;
  }

  .psd-box__warp li:last-child {
    border-right: none;
  }

  .psd-box__warp li span {
    display: inline-block;
    width: 10px;
    height: 10px;
    background: #333;
    border-radius: 50%;
  }

  .keyboard {
    position: relative;
    width: 100%;
  }
  .keyboard ul {
    background: #fff;
  }
  .keyboard ul li {
    width: 33.3%;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border-right: 1px solid #e5e5e5;
    border-bottom: 1px solid #e5e5e5;
    text-align: center;
    color: #333;
    font-size: 18px;
    box-sizing: border-box;
  }

  .border-top {
    border-top: 1px solid #e5e5e5;
  }

  .keyboard ul li:nth-of-type(3n) {
    border-right: none;
  }

  .ba-li {
    background: #dbdbdb;
  }

  .keyboard ul li.border-none {
    border-bottom: none;
  }
  .password {
    background: #fff;
    height: 700px;
  }

  .progress {
    padding: 27px 32px 5px;
  }

  .progress img {
    width: 17px;
    height: 17px;
    margin: 0;
    padding: 0;
    box-sizing: border-box;

  }

  .progress .img-box {
    width: 17px;
    height: 17px;
    border: 1px solid rgba(198, 198, 198, 1);
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    border-radius: 50%;
  }

  .line {
    border-bottom: 1px solid rgba(198, 198, 198, 1);
  }

  /* rgba(255,161,176,1); */
  .code-img {
    width: 15px;
    height: 15px;
  }

  .code-li {
    margin: 0 40px;
    height: 55px;
    border-bottom: 1px solid rgba(238, 238, 238, 1);
    font-size: 16px;
    font-family: San-Francisco-Display-Regular;
    font-weight: 400;
    color: rgba(39, 37, 37, 1);
    /* line-height:40px; */
  }

  .getcode-btn {
    padding: 0 8px;
    /* width: 76px; */
    height: 26px;
    background: rgba(255, 161, 176, 1);
    /* rgba(255,161,176,1); rgba(255, 194, 204, 1); */
    font-size: 12px;
    font-family: SourceHanSansCN-Regular;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    /* line-height:70px; */
  }

  .commit {
    margin: 50px 42px 0;
    background: rgba(255, 161, 176, 1);
    font-size: 16px;
    font-family: SourceHanSansCN-Medium;
    font-weight: 600;
    color: rgba(255, 255, 255, 1);
    height: 44px;
  }

  .checkpassword {
    margin: 30px 0;
    font-size: 12px;
    font-family: SourceHanSansCN-Regular;
    font-weight: 400;
    color: rgba(25, 25, 25, 1);
    /* line-height:30px; */
  }

  .input-password-box {}

  .password-text {
    font-size: 14px;
    font-family: SourceHanSansCN-Regular;
    font-weight: 500;
    color: rgba(25, 25, 25, 1);
    line-height: 30px;
  }

  .password-ul {
    border: 1px solid rgba(160, 154, 155, 1);
    margin: 20px 15px;
    border-radius: 5px;
  }



  .password-li {
    margin: 5px 0;
    height: 34px;
    border-right: 1px solid rgba(160, 154, 155, 1);

  }

  .password-span {
    width: 10px;
    height: 10px;
    background: rgba(25, 25, 25, 1);
    border-radius: 50%;
    /* display: inline-block; */
  }

  .progress-text {}

  .progress-text-box {
    padding: 0px 15px;
    font-size: 12px;
    font-family: SourceHanSansCN-Regular;
    font-weight: 400;
    color: rgba(102, 99, 99, 1);
    /* line-height:30px; */
    padding-bottom: 27px;
    ;
  }

  .line-red {
    border-bottom: 1px solid rgba(255, 161, 176, 1) !important;
  }

  .text-red {
    color: rgba(255, 161, 176, 1) !important;
  }

  .btn-disabled {
    background: rgba(255, 194, 204, 1) !important;
    color: rgba(255, 255, 255, 1) !important;
  }

  .input-mask {
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }

  .password-box {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .old-password {
    margin-top: 14px;
  }
</style>

<script>
  import api from "@/fetch/api";
  import ComHeader from "@/common/ComHeader";
  import ComFooter from "@/common/ComFooter";
  import Loading from "@/base/loading/loading";
  import Scroll from "@/base/scroll/scroll";
  import {
    MessageBox,
    Spinner,
    Button,
    Toast,
    Picker,
    Field,
    Indicator
  } from "mint-ui";
  export default {
    name: "PaymentPassword",
    data() {
      return {
        // 用户信息
        ethelWxUserInfo:'',

        isShowOldPassWord: true,

        // 控制流程进度
        step: 1,

        // 是否展示旧密码输入框
        isShowOldInput:false,

        // 控制输入的球
        inputMask: '',

        // 存储手机号
        phonenumber: '',

        // 存储验证码
        code: '',

        // 控制按钮颜色，是否可用
        isdisabled: false,

        // 验证码时间
        time: 60,

        // 是否展示确认按钮
        isShowCommit: true,

        // 是否显示密码输入框
        isshowpasswordbox: false,

        // 保存两次密码，判断是否一致
        password1: '',
        password2: ''

      }
    },
    components: {
      ComHeader,
      Toast,
    },
    created() {


      // 个人信息
      this.ethelWxUserInfo = window.localStorage.getItem(this.companyUnique)
        ? JSON.parse(window.localStorage.getItem(this.companyUnique))
        : "";
       this.phonenumber =  this.ethelWxUserInfo.username

    },
    mounted(){
      // 判断是否设置过密码
      this.fnHasPassWord()
    },

    methods: {
      /**
       * @author ye
       * @desc   密码输入框
       * @params {hasVideo, url}
       */
      fnHandleSetPassword(_num) {
        if (_num == '删除') {
          this.inputMask = this.inputMask.slice(0, -1)
        } else {
          this.inputMask += _num
        }
        if (this.inputMask.length == 6 && this.step == 2) {
          this.fnCommit()
        }
      },

      /**
     * @author ye
     * @desc   点击密码输入框
     * @params {hasVideo, url}
     */
      fnInput() {
        this.isshowpasswordbox = true
      },

      /**
     * @author ye
     * @desc   清空验证码
     * @params {hasVideo, url}
     */
      fnClearCode() {
        this.code = ''
      },

      /**
     * @author ye
     * @desc   获取验证码
     * @params {hasVideo, url}
     */
      fnGetCode() {
        if (this.isdisabled == true) {
          Toast("请稍后再试")
          return
        }
        this.isdisabled = true
        this.$refs.code.innerHTML = this.time-- + 's'
        var timer = setInterval(() => {
          // console.log(this.$refs.code.innerHTML)
          this.$refs.code.innerHTML = this.time-- + 's'
          if (this.time < 0) {
            this.$refs.code.innerHTML = '获取验证码'
            this.isdisabled = true
            this.isdisabled = false
            this.time = 60
            clearInterval(timer)
          }
        }, 1000)


        var params = {
          phone: this.phonenumber,
          purpose: 2,
          uid: this.ethelWxUserInfo.id
        }
        api.smspost(params)
          .catch(res => {
            console.log(888, res);
            Toast('已发送，请查看短信')

          })

      },

      /**
     * @author ye
     * @desc   验证是否设置过密码
     * @params {hasVideo, url}
     */
      fnHasPassWord() {
        // console.log(123)

        if(!this.ethelWxUserInfo.is_bind_phone){
          Toast('请先绑定手机号');

          setTimeout(()=>{
            this.routeTo('/entry/register')
          }, 1000)
          return
        }
        var params = {
          uid: this.ethelWxUserInfo.id,
          ope_type: 'check',
          has_pay_pw: 'yes'
        }
        api.userpayinfo(params)
          .catch(res => {
            console.log('判断是否设置支付密码', res.data.msg, res);
            // 未设置密码
            if (res.data.code == 1) {
              // 关闭选择入口
              this.isShowOldInput = false
              Toast(res.data.msg)
            } else if (res.data.code == 0) {
              // 打开选择入口
              this.isShowOldInput = true
              Toast('已设置支付密码，可通过身份验证，修改支付密码')
            }
          })

      },

      /**
     * @author ye
     * @desc   确定按钮
     * @params {hasVideo, url}
     */
      fnCommit() {
        if (this.inputMask.length < 6 && this.step != 1) {
          Toast('请输入完整密码')
          return
        }
        if (this.step == 1) {
          if (this.inputMask) {
            // 根据旧密码验证
            var params = {
              ope_type:'check',
              is_pay_pw:'yes',
              pay_pw:this.inputMask,
              uid:this.ethelWxUserInfo.id,
            }
            api.userpayinfo(params)
              .catch(res => {
                console.log('验证旧密码', res);
                if(res.data.code == 0){
                  this.step = 2

                }else{
                  Toast('您输入的密码错误，请重新输入')
                }
              })

          } else {
            // 根据验证码验证
            console.log('验证码对比', this.phonenumber,this.code);
            var params = {
              purpose:2,
              phone:this.phonenumber,
              auth_code:this.code,
              uid:this.ethelWxUserInfo.id
            }
            api.smsget(params)
              .catch(res => {
                if(res.data.code == 0){
                  this.step = 2
                }
                Toast(res.data.msg)
                console.log('验证码对比', res);
              })
          }
          this.inputMask = ''
          this.password1 = ''
          this.password2 = ''
        } else if (this.step == 2) {
          // 获取第一次输入的值
          this.password1 = this.inputMask

          // 清空输入框
          this.inputMask = ''

          // 进入第三步
          this.step = 3

          // 输入框对焦
          // this.$refs.inputmask.autofocus

        } else if (this.step == 3) {

          this.password2 = this.inputMask
          this.inputMask = ''
          if (this.password1 != this.password2) {
            Toast('两次密码不同，请重新输入')

            // 清空输入框
            this.inputMask = ''

            // 进入第三步
            this.step = 2

            this.password1 = ''
            this.password2 = ''

          } else {

            Toast('密码设置成功')
            var params = {
              uid: this.ethelWxUserInfo.id,
              ope_type: 'bind',
              modify_type: 'pay_pw',
              pay_pass: this.password2,
            }
            api.userpayinfo(params)
              .catch(res => {
                console.log(888, res);
              })

            // 跳出当前页面
            setTimeout(() => {
              this.$router.push({
                path: 'my',
              })

            }, 2000);
          }
        }
      }
    }
  };
</script>



<style lang="stylus" scoped>

</style>
